<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *  Store Locator  Maitai Class
 *
 * 
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    Google Maps
 * @author      Carla Missiona
 * @link
 */


class Storelocator {

    /**
     * Constructor
     *
     * @access    public
     */
    function __construct()
    {
         $this->CI =& get_instance();  
    }
    function locateStore( $city, $town, $brgy, $area ,$locator, $region=false ){

      $this->CI->load->model('store');    
       
      /* get client Location*/ 
      $this->CI =& get_instance();  

       
        if($locator == "nearest"){  
           $r =  $this->checkLocation($city, $town, $brgy, $area);
          
          
           if( $r == false ){  
           
          /* if location not in db */    
            $clientadd=""; $phil = "Philippines";
            /* compose clientadd  */  
            foreach(array($phil,$city,$town) as $k => $v ){   
                if(!empty($v)){    
                   $v = preg_replace('/\s+/', '', $v);
                   $clientadd.=$v;
                   if( $k < 3 ){
                       $clientadd.="+";
                    }    
                 }
             }
            /* fix for extra + */
            if(substr($clientadd,-1) == "+"){
                $clientadd = substr($clientadd,0,-1); 
            } 
            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=$clientadd";    

           
            $curl = curl_init();

            curl_setopt_array($curl, array(
                    CURLOPT_RETURNTRANSFER => 1,
                    CURLOPT_URL => $url
            ));
      
               $resp = curl_exec($curl);
               curl_close($curl);
               /* curl 4 lat long */

               $google = json_decode($resp); 
               $gl = get_object_vars($google); // this convert object to array
            
         /* store in db */
         
         $b = false; $lat=$gl['results'][0]->geometry->location->lat; $lng=$gl['results'][0]->geometry->location->lng ; // $lname=str_replace("+", " ", $clientadd);
         $a = array($lat, $lng,$city, $town, $brgy, $area); 
         foreach($a as $k => $v){ 
           /*  replacing spaces or empty with NULL  */
           if( $v == " " || empty($v) ){
            $a[$k] = NULL;    
           }
        } 
        
         while( $b == false ){ 
             //while error in db try repeat below          
            $b = $this->CI->map->putsClientLoc($a[0],$a[1],$a[2],$a[3],$a[4],$a[5]);
         }
         
         /* query nearest store */     
         // $markers = $this->CI->map->getNeareststore($lat,$lng,35); // moved
         $markers = $this->CI->store->getStore($lat,$lng,30); 

         //echo "markers = = >";var_dump($markers); echo "<br>";
         if(isset($gl['results'][0]->geometry->location->lat)){ 
           //die(" format 1 ");
           $m = array();
           array_push($m, array( "Lat" => $gl['results'][0]->geometry->location->lat));
           array_push($m, array( "Lng" => $gl['results'][0]->geometry->location->lng));
           array_push($m, array( "stores" => $markers));
           return $m;
           
           //return array( "Lat" => $gl['results'][0]->geometry->location->lat , "Lng" => $gl['results'][0]->geometry->location->lng , "stores" => $markers );
        }
        return " Error in user location input ";    
       } // end if not in db  
       else{  
         /*  if locations in db  call getNearest store  */  
         $n = array(); 
         foreach($r[0] as $k => $v){
            if($k == 'Lat' || $k == 'Lng' || $k == 'Longname'){
            array_push($n , array($k => $v ));
            }
         }   
        /* get markers or nearest stores */
        // var_dump($r); echo "<br>";       
           $markers = $this->CI->store->getStore($r[0]["Lat"],$r[0]["Lng"],50);
            //var_dump($markers); echo " < == Markers <br>";   
            //array_push($n,$markers);
             $n['stores'] = $markers;  
         return $n ;        
         }
   } // end locator nearest 
    else if($locator == "preferred"){
 
      $r = trim($region);         
      $response = $this->CI->store->getStore(false,false,false,$r) ;
      if( $response == false ){
          /* returns nothing */
      }
      else{       
          return $response;     
      }
       
    }
     
  }// end function get client Location 
 
    function checkLocation( $city, $town, $brgy, $area ){  
      $a = array($city, $town, $brgy, $area) ;
      foreach($a as $k => $v){ 
         /*  replacing spaces or empty with NULL  */
         if( $v == " " || empty($v) ){
          $a[$k] = NULL;    
         }
      } 
        
     
      return $this->CI->map->getClientLatlong($a[0], $a[1], $a[2], $a[3] ); 
    }
    
}

/* End of file Googlemap.php */
/* Location: ./application/libraries/Googlemap.php */